#include "StdAfx.h"
#include "GameStateController.h"
#include "RenderingManager.h"
#include "InputController.h"
#include "MeshStorage.h"
#include "RaceController.h"
#include "MainMenu.h"
#include "Intro.h"
#include "GameCommands.h"
#include <string>
#include <stdexcept>


/////////////////////////////////////////////////////////////////////////////

GameStateController::GameStateController(IGameCommands& commandsDispatcher,
                                         IRenderingManager& renderingManager,
                                         IMeshStorage& meshStorage,
                                         CInputController& inputController,
                                         ICameraImpl& cameraImpl)
      : m_commandsDispatcher(commandsDispatcher),
      m_intro(NULL),
      m_mainMenu(NULL),
      m_race(NULL),
      m_activeState(NULL)
{
   m_intro = new Intro(*this, renderingManager, meshStorage);
   m_mainMenu = new MainMenu(*this, inputController, renderingManager, meshStorage);
   m_race = new RaceController(*this, renderingManager, meshStorage, inputController, cameraImpl);

   m_activeState = m_intro;
   m_activeState->activate();
}

/////////////////////////////////////////////////////////////////////////////

GameStateController::~GameStateController(void)
{
   m_activeState = NULL;

   delete m_race;
   m_race = NULL;

   delete m_intro;
   m_intro = NULL;

   delete m_mainMenu;
   m_mainMenu = NULL;
}

/////////////////////////////////////////////////////////////////////////////

void GameStateController::update(float timeElapsed)
{
   m_activeState->update(timeElapsed);
}

/////////////////////////////////////////////////////////////////////////////

void GameStateController::openMainMenu()
{
   m_activeState->deactivate();
   m_activeState = m_mainMenu;
   m_activeState->activate();
}

/////////////////////////////////////////////////////////////////////////////

void GameStateController::startRace(const std::string& trackName)
{
   m_activeState->deactivate();
   m_activeState = m_race;
   m_trackName = trackName;
   m_activeState->activate();
}

/////////////////////////////////////////////////////////////////////////////

void GameStateController::victory(UINT playerIdx)
{
   m_activeState->deactivate();
   m_activeState = m_mainMenu;
   m_activeState->activate();
}

/////////////////////////////////////////////////////////////////////////////

void GameStateController::closeGame()
{
   m_commandsDispatcher.closeApp();
}

/////////////////////////////////////////////////////////////////////////////